package com.wanli.storemobile.merchant;


import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ethanhua.skeleton.Skeleton;
import com.ethanhua.skeleton.SkeletonScreen;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.wanli.storemobile.R;
import com.wanli.storemobile.base.BaseActivity;
import com.wanli.storemobile.base.DataCallBack;
import com.wanli.storemobile.bean.BaseResponseBean;
import com.wanli.storemobile.bean.MemberListBean;
import com.wanli.storemobile.event.MemberEvent;
import com.wanli.storemobile.homepage.model.HomePageModelImpl;
import com.wanli.storemobile.homepage.model.IHomePageModel;
import com.wanli.storemobile.merchant.adapter.MerchantMemberListAdapter;
import com.wanli.storemobile.utils.ToastUtil;
import com.wanli.storemobile.widget.AppTitleBar;
import com.wanli.storemobile.widget.CommonDialog;


import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MerchantMemberListActivity extends BaseActivity implements OnRefreshLoadMoreListener {
    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.edit_search)
    EditText editSearch;
    @BindView(R.id.view_cancel)
    View viewCancel;
    @BindView(R.id.ll_search)
    LinearLayout llSearch;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    private List<MemberListBean.DataBean.ItemListBean> beanList;
    private MerchantMemberListAdapter listAdapter;
    private int page = 1;
    private SkeletonScreen skeletonScreen;
    private IHomePageModel homePageModel;
    private String mobile = "";
    private CommonDialog.Builder dialogBuild;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_merchant_member_list);
        ButterKnife.bind(this);
        registerEventBus();
        initView();
        initData();
    }

    @Subscribe
    public void onMemberEvent(MemberEvent event) {
        refreshData();
    }

    private void initView() {
        refreshLayout.setRefreshHeader(new ClassicsHeader(mActivity));//设置Header
        refreshLayout.setRefreshFooter(new ClassicsFooter(mActivity));//设置Footer
        refreshLayout.setOnRefreshLoadMoreListener(this);
    }


    public void initData() {
        homePageModel = new HomePageModelImpl();

        beanList = new ArrayList<>();
        listAdapter = new MerchantMemberListAdapter(beanList);
        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        recyclerView.setAdapter(listAdapter);

        listAdapter.setOnCallBackListener(((type, position) -> {
            MemberListBean.DataBean.ItemListBean itemListBean = beanList.get(position);
            if (type == 1) {
                Intent intent = new Intent(mActivity, MerchantAddMemberActivity.class);
                intent.putExtra("DataBean", itemListBean);
                startActivity(intent);
            }
            if (type == 2) {
                dialogBuild = new CommonDialog.Builder(mActivity);
                dialogBuild.setTitle("提示");
                dialogBuild.setMessage("是否删除此会员?");
                dialogBuild.setPositiveButton("确定", v -> {
                    dialogBuild.dismiss();
                    requestMerchantMemberDelete(itemListBean.getMember_no(), position);
                });
                dialogBuild.setNegativeButton("取消", v -> dialogBuild.dismiss());
                dialogBuild.setCancelable(false);
                dialogBuild.show();
            }
        }));

        skeletonScreen = Skeleton.bind(recyclerView)
                .adapter(listAdapter)
                .color(R.color.shimmer_color_2)
                .shimmer(true)
                .angle(20)
                .frozen(false)
                .duration(1000)
                .count(20)
                .load(R.layout.item_skeleton_income_list)
                .show();
        requestData();
    }

    public void requestData() {
        homePageModel.requestMerchantMemberIndex(page, mobile, new DataCallBack<MemberListBean>() {
            @Override
            public void onSuccessful(MemberListBean bean) {
                if (bean.getData().getItemList() != null && bean.getData().getItemList().size() > 0) {
                    beanList.addAll(bean.getData().getItemList());
                    if (page == 1) {
                        refreshLayout.finishRefresh();
                    } else {
                        refreshLayout.finishLoadMore();
                    }
                } else {
                    if (page != 1) {
                        refreshLayout.finishLoadMoreWithNoMoreData();
                    } else {
                        refreshLayout.finishRefresh();
                    }
                }
                if (beanList.size() > 0) {
                    llNoData.setVisibility(View.GONE);
                } else {
                    llNoData.setVisibility(View.VISIBLE);
                }
                listAdapter.notifyDataSetChanged();
                if (page == 1) {
                    skeletonScreen.hide();
                }
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    public void requestMerchantMemberDelete(String member_no, int position) {
        homePageModel.requestMerchantMemberDelete(member_no, new DataCallBack<BaseResponseBean>() {
            @Override
            public void onSuccessful(BaseResponseBean bean) {
                ToastUtil.showShort("删除成功");
                beanList.remove(position);
                listAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page++;
        requestData();
        refreshLayout.finishLoadMore(30000);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refreshData();
    }

    public void refreshData() {
        page = 1;
        beanList.clear();
        requestData();
        refreshLayout.finishRefresh(30000);
    }

    @OnClick({R.id.view_cancel, R.id.tv_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.view_cancel:
                editSearch.setText("");
                mobile = "";
                refreshData();
                break;
            case R.id.tv_search:
                mobile = editSearch.getText().toString();
                if (TextUtils.isEmpty(mobile)) {
                    ToastUtil.showShort("请输入手机号");
                    return;
                }
                refreshData();
                break;
        }
    }
}
